package com.service.business.chia.controller;

import cn.jiguang.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.service.business.chia.service.SysQuotesChiaService;
import com.service.business.framework.log.RequestLog;
import com.service.business.support.MemCacheNoTran;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.dao.model.SysQuotesChia;
import com.service.dao.model.SysQuotesIpfx;
import com.service.utility.common.UUIDUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/chia")
@Api(tags = "Chia区块数据")
public class ChiaController extends BaseController {

    @Autowired
    private MemCacheNoTran memCache;

    @Autowired
    private SysQuotesChiaService sysQuotesChiaService;

    @GetMapping("/info")
    @ApiOperation("chia区块数据")
    @RequestLog("chia区块数据")
    public BaseResponse info(HttpServletRequest request) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
        String CHIA_CONFIG = memCache.get("chia_config");
        if(StringUtils.isNotEmpty(CHIA_CONFIG)){
            Object rep = JSONObject.parse(CHIA_CONFIG);
            return successQueryOneResponse(ModelMapperUtil.map(rep, SysQuotesIpfx.class));
        }else{
            //读取数据库信息
            LambdaQueryWrapper<SysQuotesChia> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.last("limit 1");
            SysQuotesChia quotesChia = sysQuotesChiaService.getOne(queryWrapper);
            return successQueryOneResponse(quotesChia);
        }
    }
}
